from data import dataset
import numpy as np


def gradAscent(dataMatIn, classLabels, alpha=0.001, maxCycles = 500, *args, **kwargs):
    def sigmoid(inX):
        return 1.0 / (1 + np.exp(-inX))

    dataMatrix = np.mat(dataMatIn)
    dataMatrix = np.insert(dataMatrix, 0, values=np.ones(dataMatrix.shape[0]), axis=1)
    labelMat = np.mat(classLabels).T
    weights = np.ones((dataMatrix.shape[1], 1))
    for i in range(maxCycles):
        h = sigmoid(dataMatrix * weights)
        err = labelMat - h
        weights = weights + alpha * dataMatrix.T * err
    return weights.getA()


if __name__ == "__main__":
    dataMatIn, classLabels = dataset.loadDataSet("./data/data.txt")
    plt, ax = dataset.Drawdata()
    weight = gradAscent(dataMatIn, classLabels, alpha=0.001, maxCycles=1000)
    print(weight)
    x = np.arange(-3.0, 3.0, 0.1)
    y = -(weight[0] + weight[1] * x) / weight[2]
    ax.plot(x, y)
    plt.show()

